package com.study.mysql.readwriteseparation1.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.study.mysql.readwriteseparation1.aop.annotation.ReadOnly;
import com.study.mysql.readwriteseparation1.entity.BusinessOrder;
import com.study.mysql.readwriteseparation1.mapper.BusinessOrderMapper;
import com.study.mysql.readwriteseparation1.route.ClientDataSourceContextHolder;
import com.study.mysql.readwriteseparation1.service.IBusinessOrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
@Slf4j
public class BusinessOrderServiceImpl extends ServiceImpl<BusinessOrderMapper, BusinessOrder>
    implements IBusinessOrderService {

    @Resource
    private BusinessOrderMapper businessOrderMapper;

    @Override
    public BusinessOrder queryMasterOrder(String s) {
        BusinessOrder businessOrder = businessOrderMapper.selectById(s);
        System.out.println("查询master库：" + ClientDataSourceContextHolder.getClientDatabase());
        return businessOrder;
    }

    @Override
    @ReadOnly
    public BusinessOrder querySlaveOrder(String s) {
        BusinessOrder businessOrder = businessOrderMapper.selectById(s);
        System.out.println("查询slave库：" + ClientDataSourceContextHolder.getClientDatabase());
        return businessOrder;
    }

    @Override
    public BusinessOrder addOrder() {
        BusinessOrder businessOrder = new BusinessOrder();
        businessOrder.setAddressId("1");
        businessOrderMapper.insert(businessOrder);
        System.out.println("查询master库：" + ClientDataSourceContextHolder.getClientDatabase());
        return businessOrder;
    }
}
